<?php
// +----------------------------------------------------------------------
// | UCToo [ Universal Convergence Technology ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2020 http://uctoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Patrick <contact@uctoo.com>
// +----------------------------------------------------------------------

namespace app\wechatopen\controller;
use app\wechatopen\service\openPlatform\AuthorizedEventHandler;
use app\wechatopen\service\openPlatform\RegisterEventHandler;
use app\wechatopen\service\openPlatform\UnauthorizedEventHandler;
use app\wechatopen\service\openPlatform\ComponentVerifyTicketEventHandler;
use app\wechatopen\service\openPlatform\UpdateAuthorizedEventHandler;
use EasyWeChat\OpenPlatform\Server\Guard;
use think\Cache;
use think\Controller;
use think\Session;
use uctoo\ThinkEasyWeChat\Facade;

/*
 * 微信开放平台授权事件处理
 * */
class Authevent extends Controller {

    protected $openPlatform;

    /*
     * 微信第三方平台授权事件接收URL
     * @param Application $app
     * */
      public function index(){
          $auth_code = input('auth_code');  //商户扫码授权后第三方平台redirect回本地址时会有auth_code字段

          if ($this->request->isGet() && !isset($auth_code)) {  //跳转第三方平台授权页,微信第三方平台接收到的地址 $_SERVER["HTTP_REFERER"] 需要与第三方平台 授权事件接收URL 相同
              $admin = Session::get('admin');
              $callbackurl = url('wechatopen/authevent/index',['admin_id'=>$admin['id']],'',true);
              $wechatopenInfoConfig = Cache::get('wechatopen_config');
              $this->openPlatform = Facade::openPlatform($wechatopenInfoConfig['app_id'],$wechatopenInfoConfig);
            //  $this->openPlatform['admin'] = Session::get('admin');
              $PAurl = $this->openPlatform->getPreAuthorizationUrl($callbackurl);
              $this->success("跳转第三方平台授权页",$PAurl,'',1);
          }
          if(isset($auth_code)){
              $admin_id = input('admin_id');
              $this->redirect('admin/wechatopen/applet','',302,['admin_id'=>$admin_id,'auth_code'=>$auth_code]);  //隐式传参骚操作
          }
              try{
                  $wechatopenInfoConfig = Cache::get('wechatopen_config');
                  $this->openPlatform = Facade::openPlatform($wechatopenInfoConfig['app_id'],$wechatopenInfoConfig);
                  // 授权事件
                  $this->openPlatform->server->push(new ComponentVerifyTicketEventHandler(),Guard::EVENT_COMPONENT_VERIFY_TICKET);
                  // 授权事件
                  $this->openPlatform->server->push(new AuthorizedEventHandler($this->openPlatform),Guard::EVENT_AUTHORIZED);
                  // 更新授权事件
                  $this->openPlatform->server->push(new UpdateAuthorizedEventHandler($this->openPlatform),Guard::EVENT_UPDATE_AUTHORIZED);
                  // 取消授权事件
                  $this->openPlatform->server->push(new UnauthorizedEventHandler(),Guard::EVENT_UNAUTHORIZED);
                  // 小程序快速注册事件
                  $this->openPlatform->server->push(new RegisterEventHandler(),Guard::EVENT_THIRD_FAST_REGISTERED);

                  $this->openPlatform->server->serve()->send();

              }catch (\Exception $e){
                  return '404';
              }
    }
}